#include <errno.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>

#include "yaml.h"

#include "somb.h"
#include "somb_conf.h"

int32_t somb_config_file_parser(char *conf, ST_SOMB_DAEMON_CONF *pstConf) {
  int32_t ret;
  FILE *file;
  yaml_parser_t parser;
  yaml_token_t token;
  int done = 0;

  file = fopen(conf, "rb");
  if (NULL == file) {
    printf("somb daemon: open somb config file[%s] failed\n", conf);
    return -1;
  }

  ret = yaml_parser_initialize(&parser);
  if (1 != ret) {
    printf("somb daemon: somb config file[%s] parser init failed\n", conf);
    return -1;
  }

  yaml_parser_set_input_file(&parser, file);
  int state = 0;
  char *key_charp = NULL;
  // int8_t* key_int8p=NULL;
  int32_t *key_int32p = NULL;
  char *tk;
  while (!done) {
    if (!yaml_parser_scan(&parser, &token)) {
      break;
    }

    // printf("token.type=%d\n",token.type);
    switch (token.type) {
    case YAML_KEY_TOKEN:
      state = 0;
      break;
    case YAML_VALUE_TOKEN:
      state = 1;
      break;
    case YAML_SCALAR_TOKEN:
      tk = token.data.scalar.value;
      if (state == 0) {
        if (strcmp(tk, "LOG_PATH") == 0) {
          key_charp = (char *)&(pstConf->log_path[0]);
        } else if (strcmp(tk, "LOG_LEVEL") == 0) {
          key_int32p = &(pstConf->log_level);
        } else if (strcmp(tk, "LOG_FILE_SIZE") == 0) {
          key_int32p = &(pstConf->log_file_size);
        } else if (strcmp(tk, "LOG_FILE_NUM") == 0) {
          key_int32p = &(pstConf->log_file_num);
        } else if (strcmp(tk, "LOG_COMP") == 0) {
          key_int32p = &(pstConf->log_comp);
        } else {
          // printf("unkonw key=%s\n", tk);
        }
      } else {
        if (NULL != key_charp) {
          sprintf(key_charp, "%s", tk);
          key_charp = NULL;
        } else if (NULL != key_int32p) {
          sscanf(tk, "%d", key_int32p);
          key_int32p = NULL;
        } else {
          // printf("unknown key's value=%s\n", tk);
        }
      }
      break;
    case YAML_STREAM_END_TOKEN:
      done = 1;
      break;
    default:
      // printf("unknown token type=%d\n", token.type);
      break;
    }

    yaml_token_delete(&token);
  }

  yaml_parser_delete(&parser);
  fclose(file);

  if (conf[0] == '/') {
    sprintf(pstConf->conf_path, "%s", conf);
  } else {
    char bufp[SOMB_CONF_PATH_LENGTH];
    bzero(bufp, SOMB_CONF_PATH_LENGTH);
    getcwd(bufp, SOMB_CONF_PATH_LENGTH);
    sprintf(pstConf->conf_path, "%s/%s", bufp, conf);
  }
#if 0
  int count = 0;
  int error = 0;
    printf("somb :config file parse result:%s (%d events)\n", (error ? "FAILURE" : "SUCCESS"), count);
    printf("somb :conf_path=%s\n",pstConf->conf_path);
    printf("somb :log_path=%s\n",pstConf->log_path);
    printf("somb :log_level=%d\n",pstConf->log_level);
    printf("somb :log_file_size=%d\n",pstConf->log_file_size);
    printf("somb :log_file_num=%d\n",pstConf->log_file_num);
#endif

  return SOMB_OK;
}
